package base.java.util;





//Standard JDK Imports

import java.io.InputStream;

import java.io.IOException;

import java.util.Properties;

import java.util.Enumeration;





/**

* A utility class for loading a property file and then

* retrieving the values for a given key.

*

* @author 

* @version    0.1

*/

public class PropertyReader

{



	/** The java.util.Properties object */

	private Properties m_pProperties;

	

	/** Line separator */

	private static String LS = System.getProperty("line.separator");


	
	/**PropertyReader instance*/
	private static PropertyReader prInstance ;

 /**

	 * Loads the property file that is passed as a parameter

	 *

	 * @param sFileName  The Property file that needs to be loaded

	 * @throws  in case an error happens while loading 

	 * the specified property file

	 */

	public PropertyReader(String sFileName) 

	{



		InputStream inputStream = getClass().getResourceAsStream("/" + sFileName);

		m_pProperties = new Properties();

		try

		{

		   m_pProperties.load(inputStream);

		}

		catch (IOException ioe)

		{
			ioe.printStackTrace();
		}

	}
	
	public static String getCommonPropertyValue(String key, String defaultKey){
		PropertyReader p = new PropertyReader("SystemConfig.properties");
		return p.getPropertyValue(key, defaultKey);
	}



 /**

	 * Returns the Value for the requested key by querying the properties file

	 *

	 * @param sKey  The key for which the value needs to be retrieved

	 * @return String the value in this property list with the

	 * specified key value.

	 */

	public String getPropertyValue(String sKey)

	{

		return m_pProperties.getProperty(sKey);

	}



 /**

	 * Returns the Value for the requested key by querying the properties file, if

	 * no value is present, then the default value will be returned

	 *

	 * @param sKey  The key for which the value needs to be retrieved

	 * @param sDefaultValue  The default value

	 * @return String the value in this property list with the

	 * specified key value.

	 */

	public String getPropertyValue(String sKey, String sDefaultValue)

	{

		String rtn = null;
		try
		{
			rtn = new String((m_pProperties.getProperty(sKey, sDefaultValue)).getBytes("iso8859-1"));
		}
		catch(Exception e)
		{
			return "";	
		}
		return rtn;
	}



 /**

	 * Returns the Enumeration containing the list of all property names

	 *

	 * @return Enumeration enumeration of all property names

	 */



	public Enumeration getPropertyNames()

	{

		return m_pProperties.propertyNames();

	}
	
	/**
	 * create PropertyReader instance
	 * @return PropertyReader
	 */
	public static PropertyReader getInstance(String propertyFile)
	{
		if(prInstance == null){
			prInstance = new PropertyReader(propertyFile);
		}
		return prInstance ;
	}
	

} //end of class